package com.catchu.pattern.creational.适配器模式.基于对象;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 操作用户的实际类
 */
public class UserOperation {

    private static final Map<String,Integer> userMap = new HashMap<>(16);

    public UserOperation() {
        initUserMap();
    }

    /**
     * 初始化
     */
    private void initUserMap(){
        userMap.put("张三",1);
    }

    /**
     * 插入用户
     * @param userName
     * @return
     */
    public boolean insertUser(String userName){
        boolean flag = false;
        try {
            userMap.put(userName,getMaxSn()+1);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 获取最大编号
     * @return
     */
    public int getMaxSn(){
        return Collections.max(userMap.values());
    }

    /**
     * 根据用户名称获取其编号
     * @param userName
     * @return
     */
    public int getSnByName(String userName){
        //用户流水号
        int sn = 0;
        if(userMap.containsKey(userName)){
            sn = userMap.get(userName);
        }
        return sn;
    }
}
